﻿import ko = require("knockout");
import Track = require("Track");

class Clip {
    public constructor(public clip: EditoneLibRT.Clip) {
        clip.addEventListener("propertychanged", (prop: string) => this.onPropertyChanged(prop));
        this.duration = ko.observable(clip.duration);
        this.type = ko.observable(clip.type);
        this.isAudio = ko.observable(clip.isAudioClip);
        this.streamCounter = ko.observable(0);
    }

    public duration: KnockoutObservable<number>;
    public type: KnockoutObservable<string>;
    public isAudio: KnockoutObservable<boolean>;
    public streamCounter: KnockoutObservable<number>; //This is a dummy observable just to notify subscribers of a changed stream

    public move(newtrack: Track, offset: number) {
        this.clip.move(newtrack.track, offset);
    }

    fadeIn: () => void = () => {
        this.clip.fadeIn();
    }

    fadeOut: () => void = () => {
        this.clip.fadeOut();
    }

    normalize: () => void = () => {
        this.clip.normalize();
    }

    delete: () => void = () => {
        this.clip.delete();
    }

    split: (at: number) => EditoneLibRT.Clip[] = (at) => {
        return this.clip.split(at);
    }

    isLoading() : boolean {
        return this.clip.isLoading;
    }

    private onPropertyChanged(prop: string){
        prop = prop.charAt(0).toLowerCase() + prop.slice(1);
        if (prop in this) {
            this[prop](this.clip[prop]);
        } else if (prop == "stream") {
            this.streamCounter(this.streamCounter() + 1);
        }
    }

}

export = Clip;